package com.py.korona.providers.base;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import com.py.korona.db.DBHelper;

public abstract class DBContentProvider extends ContentProvider {
    private DBHelper   dbHelper_;
    private UriMatcher uriMatcher_;

    public DBContentProvider() {
        initialize();
        uriMatcher_ = new UriMatcher(UriMatcher.NO_MATCH);
        this.addAvailableURI(uriMatcher_);
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int result = 0;
        SQLiteDatabase db = dbHelper_.getWritableDatabase();
        //
        //where clause
        String whereClause = this.getSQLDeleteWhereClause(uriMatcher_, uri, selection, selectionArgs);
        //
        //table name
        String tableName = this.getSQLTableName(uriMatcher_, uri);
        if (TextUtils.isEmpty(whereClause)) {
            result = db.delete(tableName, selection, selectionArgs);
        } else {
            result = db.delete(tableName, whereClause, selectionArgs);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return result;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        Uri result = null;
        if (this.insertAllowed(uriMatcher_, uri, values)) {
            SQLiteDatabase db = dbHelper_.getWritableDatabase();
            //
            //table name
            String tableName = this.getSQLTableName(uriMatcher_, uri);
            //
            //inserting
            long id = db.insert(tableName, null, values);
            getContext().getContentResolver().notifyChange(uri, null);
            result = this.insertUri(id);
        }
        return result;
    }

    @Override
    public boolean onCreate() {
        dbHelper_ = new DBHelper(getContext());
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] propertiesToFetch, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        //
        //table name
        String tableNames = this.getSQLTableName(uriMatcher_, uri);
        builder.setTables(tableNames);
        //
        //where clause
        String where = this.getSQLSelectWhereClause(uriMatcher_, uri, selection, selectionArgs);
        if (!TextUtils.isEmpty(where)) {
            builder.appendWhere(where);
        }
        String groupbyFields = this.getSQLGroupbyFields(uriMatcher_, uri);
        //
        //building
        SQLiteDatabase db = dbHelper_.getWritableDatabase();
        Cursor cursor = builder.query(db, propertiesToFetch, selection, selectionArgs, groupbyFields, null, sortOrder);
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        SQLiteDatabase db = dbHelper_.getWritableDatabase();
        String updateWhereClause = this.getSQLUpdateWhereClause(uriMatcher_, uri, selection, selectionArgs);
        String tableName = this.getSQLTableName(uriMatcher_, uri);
        int result = db.update(tableName, values, updateWhereClause, selectionArgs);
        getContext().getContentResolver().notifyChange(uri, null);
        return result;
    }

    protected abstract void addAvailableURI(UriMatcher uriMatcher);

    protected abstract void allColumnNames();

    protected abstract String fieldNameID();

    protected abstract String getSQLDeleteWhereClause(UriMatcher uriMatcher, Uri uri, String selection, String[] selectionArgs);

    protected abstract String getSQLGroupbyFields(UriMatcher uriMatcher, Uri uri);

    protected abstract String getSQLSelectWhereClause(UriMatcher uriMatcher, Uri uri, String selection, String[] selectionArgs);

    protected abstract String getSQLTableName(UriMatcher uriMatcher, Uri uri);

    protected abstract String getSQLUpdateWhereClause(UriMatcher uriMatcher, Uri uri, String selection, String[] selectionArgs);

    protected abstract String getUriAuthority();

    protected abstract String getUriBasePath();

    protected abstract void initialize();

    protected abstract boolean insertAllowed(UriMatcher uriMatcher, Uri uri, ContentValues values);

    protected abstract Uri insertUri(long id);
}
